package com.wisdomparking.control.parking.siwo;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;
import com.wisdomparking.bean.VehicleInfo;
import com.wisdomparking.config.mvc.BaseController;
import com.wisdomparking.config.mvc.Result;
import com.wisdomparking.config.mvc.ResultGenerator;
import com.wisdomparking.service.OrdersService;
import com.wisdomparking.service.ParkingService;
import com.wisdomparking.service.VehicleInfoService;
import com.wisdomparking.service.siwo.impl.SiwoDataServiceImpl;
import com.wisdomparking.service.siwo.resbean.CarMaster;
import com.wisdomparking.service.siwo.resbean.Res;
import com.wisdomparking.tools.ToolService;
import com.wisdomparking.tools.ToolValid;

/**
 * 斯沃数据处理
 */
@RestController
@RequestMapping("/parking/siwo")
public class SiwoController extends BaseController {

	@Autowired SiwoDataServiceImpl siwoDataService;
	@Autowired VehicleInfoService vehicleInfoService;
	@Autowired OrdersService ordersService;
	@Autowired ParkingService parkingService;

	/**
	 * 入场车辆信息 -- 【斯沃】
	 * 
	 * @param plate
	 * @param identification
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/carin")
	public Result<Boolean> carin(
			@RequestParam(value = "plateNumber", required = true) String plateNumber,
			@RequestParam(value = "identification", required = true) String identification)
			throws IOException {
		Gson gson = new Gson();
		Res res = siwoDataService.getCarOwnerProfiles(plateNumber);
		
		CarMaster carmaster = null;
		
		if(!res.getMsg().equals("data error")) {
			carmaster = gson.fromJson(gson.toJson(res.getData()), CarMaster.class);
			if("月租用户"==carmaster.getCardType()){
				ToolService.generateOrder(ordersService, vehicleInfoService, parkingService, plateNumber, identification, true);
			}
			else {
				ToolService.generateOrder(ordersService, vehicleInfoService, parkingService, plateNumber, identification, false);
			}
		}
		else {
			ToolService.generateOrder(ordersService, vehicleInfoService, parkingService, plateNumber, identification, false);
		}
		
		return ResultGenerator.genSuccessResult(true);
	}
	
	/**
	 * 车辆出场是否要抬杆 
	 * 
	 * @param plateNumber
	 * @param identification
	 * @return
	 */
	@RequestMapping("/carout")
	public Result<Boolean> getUserType(String plateNumber, 
			@RequestParam(value = "identification", required = true)String identification){
		boolean flag = false;
		VehicleInfo vehicleInfo = vehicleInfoService.getVehicleInfoByPlateNumber(plateNumber);
		if(ToolValid.isNotNull(vehicleInfo)) {
			if(ToolValid.isNotNull(vehicleInfo.getUserId())) {
				flag = true;
			}
			return ResultGenerator.genSuccessResult(flag);
		}
		
		Gson gson = new Gson();
		Res res = siwoDataService.getCarOwnerProfiles(plateNumber);
		CarMaster carmaster = gson.fromJson(gson.toJson(res.getData()), CarMaster.class);
		if(ToolValid.isNotNull(carmaster)) {
			if("月租用户".equals(carmaster.getCardType())) {
				flag = true;
			}
		}
		return ResultGenerator.genSuccessResult(flag);
	}

}
